package com.liuyucheng.idea_springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuyucheng.idea_springboot.bean.DataInteraction;
import com.liuyucheng.idea_springboot.bean.LycDemoPo;
import com.liuyucheng.idea_springboot.dao.StockPileExtMapper;
import com.liuyucheng.idea_springboot.service.StockPileService;
import com.liuyucheng.idea_springboot.util.ExcelUtil;
import com.liuyucheng.idea_springboot.util.PageQuery;
import com.liuyucheng.idea_springboot.util.PubUtils;
import com.liuyucheng.idea_springboot.util.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StockPileServiceImpl implements StockPileService {

    @Resource
    private StockPileExtMapper mapper;

    @Override
    public ResultUtil<List<Map<String, Object>>> initComboData() {
        List<Map<String, Object>> list = mapper.getComboData();

        return ResultUtil.success(list);
    }

    @Override
    public PageInfo table(PageQuery<DataInteraction> pageQuery) {
        DataInteraction paramBean = pageQuery.getData();
        PageInfo pageInfo = pageQuery.getPageInfo();
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<Map<String, Object>> result = mapper.queryPage(paramBean);
        return new PageInfo(result);
    }

    @Override
    public void exportExcel(PageQuery<DataInteraction> pageQuery, HttpServletResponse response) {
        DataInteraction paramBean = pageQuery.getData();
        List<Map<String, Object>> result = mapper.queryPage(paramBean);

        //excel标题
        String[] title = {"id", "表名"};
        //excel数据
        String[][] content = new String[result.size()][];
        for (int i = 0; i < result.size(); i++) {
            content[i] = new String[title.length];
            Map<String, Object> map = result.get(i);
            content[i][0] = map.get("ID").toString();
            content[i][1] = map.get("TABLE_NAME").toString();
        }

        ExcelUtil.getXSSFWorkbook("stock.xlsx", title, content, response);
    }

    @Override
    public PageInfo demoTable(PageQuery<LycDemoPo> pageQuery) {
        LycDemoPo paramBean = pageQuery.getData();
        PageInfo pageInfo = pageQuery.getPageInfo();
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<Map<String, Object>> result = mapper.demoQueryPage(paramBean);
        return new PageInfo(result);
    }

    @Override
    public boolean saveOrUpdate(LycDemoPo po) {
        System.out.println(po);
        int resultCount = 0;
        if(po.getId()==null){//新增
            resultCount = mapper.save(po);
        }else{//编辑
            //修改附件时，原有的附件如果存在，而且新的fileId和原来的不同，把原来的删除
            String newFileId = po.getFileId();

            resultCount = mapper.update(po);
        }
        if(resultCount==1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean delete(String[] ids) {
        //删除附件时，对应的附件如果存在，删除
        int fileCount = mapper.deleteFile(ids);
        //删除记录
        int count = mapper.delete(ids);
        System.out.println("删除相关附件"+fileCount+"条");
        if(count>0){
            return true;
        }
        return false;
    }

    @Override
    public Map<String,String> uploadFile(MultipartFile file) {
        Map<String,Object> fileBean = new HashMap<>();
        try {
            //将文件转为字节数组
            byte[] arr = file.getBytes();
            long size = file.getSize();
            String filename = file.getOriginalFilename();

            long id = System.currentTimeMillis();
            fileBean.put("id",id);
            fileBean.put("file",arr);
            fileBean.put("size",size);
            fileBean.put("tablename","lycdemo");
            fileBean.put("filename",filename);

            int count = mapper.uploadFile(fileBean);
            Map<String,String> res = new HashMap<>();
            res.put("id",id+"");
            res.put("filename",filename);
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void downloadFile(String fileId, HttpServletResponse response) {
        Map<String,Object> fileBean = mapper.downloadFile(fileId);
        String fileName = fileBean.get("filename").toString();
        byte[] arr = (byte[]) fileBean.get("file");
        PubUtils.writeBlob(response,fileName,arr);
    }


}
